<template>
  <el-dialog
    :title="!dataForm.activeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="秒杀活动名称" prop="activeName">
      <el-input v-model="dataForm.activeName" placeholder="秒杀活动名称"></el-input>
    </el-form-item>
    <el-form-item label="秒杀活动价格" prop="activePrice">
      <el-input v-model="dataForm.activePrice" placeholder="秒杀活动价格"></el-input>
    </el-form-item>
    <el-form-item label="生效时间" prop="takeeffect">
      <el-input v-model="dataForm.takeeffect" placeholder="生效时间"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="beoverdue">
      <el-input v-model="dataForm.beoverdue" placeholder="过期时间"></el-input>
    </el-form-item>
    <el-form-item label="活动体系状态" prop="activeState">
      <el-input v-model="dataForm.activeState" placeholder="活动体系状态"></el-input>
    </el-form-item>
    <el-form-item label="活动分享" prop="share">
      <el-input v-model="dataForm.share" placeholder="活动分享"></el-input>
    </el-form-item>
    <el-form-item label="参与活动返奖金" prop="participatein">
      <el-input v-model="dataForm.participatein" placeholder="参与活动返奖金"></el-input>
    </el-form-item>
    <el-form-item label="图片" prop="prc">
      <el-input v-model="dataForm.prc" placeholder="图片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          activeId: 0,
          activeName: '',
          activePrice: '',
          takeeffect: '',
          beoverdue: '',
          activeState: '',
          share: '',
          participatein: '',
          prc: ''
        },
        dataRule: {
          activeName: [
            { required: true, message: '秒杀活动名称不能为空', trigger: 'blur' }
          ],
          activePrice: [
            { required: true, message: '秒杀活动价格不能为空', trigger: 'blur' }
          ],
          takeeffect: [
            { required: true, message: '生效时间不能为空', trigger: 'blur' }
          ],
          beoverdue: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          activeState: [
            { required: true, message: '活动体系状态不能为空', trigger: 'blur' }
          ],
          share: [
            { required: true, message: '活动分享不能为空', trigger: 'blur' }
          ],
          participatein: [
            { required: true, message: '参与活动返奖金不能为空', trigger: 'blur' }
          ],
          prc: [
            { required: true, message: '图片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.activeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.activeId) {
            this.$http({
              url: this.$http.adornUrl(`/shop/active/info/${this.dataForm.activeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.activeName = data.active.activeName
                this.dataForm.activePrice = data.active.activePrice
                this.dataForm.takeeffect = data.active.takeeffect
                this.dataForm.beoverdue = data.active.beoverdue
                this.dataForm.activeState = data.active.activeState
                this.dataForm.share = data.active.share
                this.dataForm.participatein = data.active.participatein
                this.dataForm.prc = data.active.prc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/active/${!this.dataForm.activeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'activeId': this.dataForm.activeId || undefined,
                'activeName': this.dataForm.activeName,
                'activePrice': this.dataForm.activePrice,
                'takeeffect': this.dataForm.takeeffect,
                'beoverdue': this.dataForm.beoverdue,
                'activeState': this.dataForm.activeState,
                'share': this.dataForm.share,
                'participatein': this.dataForm.participatein,
                'prc': this.dataForm.prc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
